package com.ycy.lucene.field;

import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;

@Slf4j
public class DocVauleDemo {

//  public static String indexpath = "C:\\Users\\Administrator\\Desktop\\ycy\\lucenedata\\ix";
  public static String indexpath = "/Users/ycy/Desktop/linshi/lucenedata/ix1";

  public static void main(String[] args) {
    DocVauleDemo docVauleDemo = new DocVauleDemo();
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入一行文本:");
    String inputLine = scanner.nextLine();
    while (!"q".equals(inputLine)) {
      log.debug("输入 -> {}",inputLine);
      switch (inputLine) {
        case "1":
          docVauleDemo.createIndex();
          break;
        case "2":
          docVauleDemo.read();
          break;
        case "3":
          docVauleDemo.readAll();
          break;
        default:
          System.out.println("输入有误");
          break;
      }
      inputLine = scanner.nextLine();
    }

  }

  public void createIndex() {
    try {

      Directory directory = FSDirectory.open(Paths.get(indexpath)); //打开
      IndexWriterConfig indexWriterConfig = new IndexWriterConfig();//写索引的时候可以设置，比如openmode
      indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND); //新建一个索引，覆盖老索引
      IndexWriter writer = new IndexWriter(directory, indexWriterConfig);

      //每一个Filed 都声明了一个FiledType
//      FieldType fieldType = new FieldType();
//      fieldType.setDocValuesType(DocValuesType.NUMERIC);
//      fieldType.setStored(true);
      for (int i = 0; i < 100; i++) {
        Document document = new Document();
        Field productId = new SortedDocValuesField("productId", new BytesRef(i));
        Field productId1 = new StoredField("productId", i);
        document.add(productId);
        document.add(productId1);

        Field price = new NumericDocValuesField("price", i);
        Field price1 = new StoredField("price", i);
        document.add(price);
        document.add(price1);

        writer.addDocument(document);
      }
      writer.commit();
      writer.close();
      System.out.println("建立索引完毕");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public void readAll() {
    try {
      FSDirectory open = FSDirectory.open(Paths.get(indexpath));
      IndexReader indexReader = DirectoryReader.open(open);
      IndexSearcher indexSearcher = new IndexSearcher(indexReader);
      Query range = NumericDocValuesField.newSlowRangeQuery("price", 0, 200);
      TopDocs search = indexSearcher.search(range, 200);
      for (ScoreDoc scoreDoc : search.scoreDocs) {
        Document doc = indexSearcher.doc(scoreDoc.doc);
        System.out.println(doc);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * 这个打印不出来结果，有问题
   */
  public void read() {
    try {
      FSDirectory open = FSDirectory.open(Paths.get(indexpath));
      IndexReader indexReader = DirectoryReader.open(open);
      SortedDocValues price = DocValues.getSorted(indexReader.leaves().get(0).reader(), "productId");
      System.out.println("productId" + price.toString());
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}
